﻿using HHCloud.Model.Models;
using HHXX.Common;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace HHXX.EFDAL
{
    public class DbContextFactory
    {
        /// <summary>
        /// 这里无法用autofac容器生成DbContext, CallContext相当于容器的效果
        /// </summary>
        /// <returns></returns>
        public static DbContext GetCurrentDbContext()
        {
            //一次请求共用一个实例 上下文都可以做到切换
            //return new DataModelContainer();
            DbContext db = (DbContext)CallContext.GetData("DbContext");
            if (db == null)
            {
                db = new HHXXDBContext();
                CallContext.SetData("DbContext", db);
            }
            return db;
        }

    }
}
